import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;

public class GoodsRemoveDumplicate extends UDF{

	public JSONObject evaluate(String jsonString) {
		//将url和title拼接成jsonString 进行处理找出相似度
		if(StringUtils.isNotBlank(jsonString)) {
			List<Map<String, Map<String, String>>> result = new ArrayList<Map<String,Map<String, String>>>();
			//转成jsonObject
			JSONObject obj = JSON.parseObject(jsonString);
			//循环取出key value
			Iterator<Entry<String, Object>> iterator = obj.entrySet().iterator();
			while(iterator.hasNext()) {
				//创建map存放相似度信息
				Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
				Entry<String, Object> entry = iterator.next();
				String key = entry.getKey();
				String value = entry.getValue().toString();
				//再次循环取出作比较
				Iterator<Entry<String, Object>> iterator1 = obj.entrySet().iterator();
				while(iterator1.hasNext()) {
					Entry<String, Object> next1 = iterator1.next();
					String keyConp = next1.getKey();
					String valueConp = next1.getValue().toString();
					//key是url value是title
					//如果url相同是同一商品 则跳过 
					if(StringUtils.equals(key, keyConp)) {
						continue;
					}else {
						if(StringUtils.equals(value, valueConp)) {
							//title完全相同
							Map<String, String> map1 = new HashMap<String, String>();
							//存放比较url 和 相似度
							map1.put(keyConp, "1.00");
							//存放原url 
							map.put(key, map1);
							//添加到list
							result.add(map);
						} /*
							 * else if(value.contains(valueConp)) { //包含 Map<String, String> map1 = new
							 * HashMap<String, String>(); map1.put(keyConp, "1"); map.put(key, map1);
							 * //添加到list result.add(map); }
							 */else {
							Map<String, String> map1 = new HashMap<String, String>();
							
							float f = CompareLikeUtils.getSimilarityRatio(value, valueConp);
							DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
							String p =decimalFormat.format(f);//format 返回的是字符串
							map1.put(keyConp,p);
							map.put(key, map1);
							//添加到list
							result.add(map);
						}
						
					}
				}
			}
			
			
			
			
			
			
			System.out.println(obj.size());
			
		}
		
		return null;
	}
	
}
